const HashTable = require("../template")

function testHashTablePerformance() {
  console.log(
    "1. 插入性能测试:",
    (() => {
      const ht = new HashTable()
      const iterations = 10000
      
      const start = performance.now()
      
      for (let i = 0; i < iterations; i++) {
        ht.set(`key${i}`, `value${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return ht.size() === iterations && 
             duration < 1000 && // 应该在1秒内完成
             duration > 0
    })()
  )

  console.log(
    "2. 查找性能测试:",
    (() => {
      const ht = new HashTable()
      const iterations = 10000
      
      // 先插入数据
      for (let i = 0; i < iterations; i++) {
        ht.set(`key${i}`, `value${i}`)
      }
      
      const start = performance.now()
      
      // 查找所有数据
      for (let i = 0; i < iterations; i++) {
        ht.get(`key${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 500 && // 查找应该很快
             duration > 0
    })()
  )

  console.log(
    "3. 删除性能测试:",
    (() => {
      const ht = new HashTable()
      const iterations = 5000
      
      // 先插入数据
      for (let i = 0; i < iterations; i++) {
        ht.set(`key${i}`, `value${i}`)
      }
      
      const start = performance.now()
      
      // 删除一半数据
      for (let i = 0; i < iterations / 2; i++) {
        ht.delete(`key${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return ht.size() === iterations / 2 &&
             duration < 500 &&
             duration > 0
    })()
  )

  console.log(
    "4. 批量操作性能测试:",
    (() => {
      const ht = new HashTable()
      const batchSize = 1000
      
      const start = performance.now()
      
      // 批量插入
      for (let i = 0; i < batchSize; i++) {
        ht.set(`key${i}`, `value${i}`)
      }
      
      // 批量查找
      for (let i = 0; i < batchSize; i++) {
        ht.get(`key${i}`)
      }
      
      // 批量删除
      for (let i = 0; i < batchSize / 2; i++) {
        ht.delete(`key${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return ht.size() === batchSize / 2 &&
             duration < 200 &&
             duration > 0
    })()
  )

  console.log(
    "5. 内存使用效率测试:",
    (() => {
      const ht = new HashTable(16, 0.75)
      
      // 插入数据直到接近负载因子
      for (let i = 0; i < 12; i++) { // 12/16 = 0.75
        ht.set(`key${i}`, `value${i}`)
      }
      
      const loadFactor = ht.getLoadFactor()
      const capacity = ht.getCapacity()
      
      return loadFactor === 0.75 &&
             capacity === 16 &&
             ht.size() === 12
    })()
  )

  console.log(
    "6. 哈希函数分布测试:",
    (() => {
      const ht = new HashTable(10)
      const testKeys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
      
      // 计算哈希值分布
      const hashValues = testKeys.map(key => ht.hash(key))
      const uniqueHashes = new Set(hashValues)
      
      // 哈希值应该在有效范围内
      const validHashes = hashValues.every(hash => 
        hash >= 0 && hash < ht.getCapacity()
      )
      
      return validHashes &&
             uniqueHashes.size > 1 && // 不应该所有键都哈希到同一个值
             hashValues.length === testKeys.length
    })()
  )

  console.log(
    "7. 并发操作模拟测试:",
    (() => {
      const ht = new HashTable()
      const operations = 1000
      
      const start = performance.now()
      
      // 模拟并发操作：交替插入和查找
      for (let i = 0; i < operations; i++) {
        if (i % 2 === 0) {
          ht.set(`key${i}`, `value${i}`)
        } else {
          ht.get(`key${i - 1}`)
        }
      }
      
      const end = performance.now()
      const duration = end - start
      
      return ht.size() === operations / 2 &&
             duration < 300 &&
             duration > 0
    })()
  )

  console.log(
    "8. 大数据量性能测试:",
    (() => {
      const ht = new HashTable()
      const largeSize = 50000
      
      const start = performance.now()
      
      // 插入大量数据
      for (let i = 0; i < largeSize; i++) {
        ht.set(`key${i}`, `value${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      // 验证数据完整性
      let allCorrect = true
      for (let i = 0; i < Math.min(1000, largeSize); i++) { // 抽样检查
        if (ht.get(`key${i}`) !== `value${i}`) {
          allCorrect = false
          break
        }
      }
      
      return allCorrect &&
             ht.size() === largeSize &&
             duration < 2000 && // 大数据量应该在2秒内完成
             duration > 0
    })()
  )

  console.log(
    "9. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(size) {
          const ht = new HashTable()
          const start = performance.now()
          
          for (let i = 0; i < size; i++) {
            ht.set(`key${i}`, `value${i}`)
          }
          
          const end = performance.now()
          return (end - start) * this.multiplier
        }
      }
      
      const result = perfObj.processPerformance(1000)
      return result > 0 && result < 1000
    })()
  )

  console.log(
    "10. 复杂对象this绑定性能测试:",
    (() => {
      const perfConfigObj = {
        config: { threshold: 1000 },
        processWithConfig: function(size) {
          const ht = new HashTable()
          const start = performance.now()
          
          for (let i = 0; i < size; i++) {
            ht.set(`key${i}`, `value${i}`)
          }
          
          const end = performance.now()
          return (end - start) < this.config.threshold
        }
      }
      
      return perfConfigObj.processWithConfig(5000) === true
    })()
  )

  console.log(
    "11. 性能一致性测试:",
    (() => {
      const consistencyHt = new HashTable()
      const iterations = 1000
      
      const start1 = performance.now()
      for (let i = 0; i < iterations; i++) {
        consistencyHt.set(`key${i}`, `value${i}`)
      }
      const end1 = performance.now()
      
      const start2 = performance.now()
      for (let i = 0; i < iterations; i++) {
        consistencyHt.get(`key${i}`)
      }
      const end2 = performance.now()
      
      const insertTime = end1 - start1
      const searchTime = end2 - start2
      
      return insertTime > 0 &&
             searchTime > 0 &&
             searchTime < insertTime && // 查找应该比插入快
             consistencyHt.size() === iterations
    })()
  )

  console.log(
    "12. 极端性能测试:",
    (() => {
      const extremeHt = new HashTable(1) // 最小容量，强制冲突
      const iterations = 1000
      
      const start = performance.now()
      
      // 在极端条件下插入数据
      for (let i = 0; i < iterations; i++) {
        extremeHt.set(`key${i}`, `value${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      // 验证数据完整性
      let allCorrect = true
      for (let i = 0; i < iterations; i++) {
        if (extremeHt.get(`key${i}`) !== `value${i}`) {
          allCorrect = false
          break
        }
      }
      
      return allCorrect &&
             extremeHt.size() === iterations &&
             duration < 1000 && // 即使在极端条件下也应该在合理时间内完成
             duration > 0
    })()
  )
}

testHashTablePerformance()
